package com.kehongborui.spring.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief 实现 spring HandlerExceptionResolver 来自定义异常处理器
 * @email 122162005@qq.com
 * @date 2018-08-02
 */
public class MyHandlerExecptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object o, Exception e) {
        if (e instanceof SpringException) {
            request.setAttribute("error_msg", e.getMessage());
        } else if (e instanceof RuntimeException) {
            request.setAttribute("error_msg", "程序员小哥开小差！");
        }
        return new ModelAndView("exception");
    }
}
